<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/cookie.js"></script>
</head>

<body>
    <!--  onsubmit="return false"    阻止表单默认提交 -->
    <form onsubmit="return false">
        <p><label for="">用户名:</label><input name="user" type="text"></p>
        <p><label for="">密&emsp;码:</label><input name="pwd" type="text"></p>
        <p><label><input type="checkbox" class="isRem">是否记住本账号?</label></p>
        <p><button class="logBtn">登录</button></p>
    </form>
</body>
<script>




    var userInp = document.getElementsByName("user")[0];
    var pwdInp = document.getElementsByName("pwd")[0];
    var logBtn = document.getElementsByClassName("logBtn")[0];
    var isRem = document.getElementsByClassName("isRem")[0];




    logBtn.onclick = function () {
        // 用户输入的

        var user = userInp.value;
        var pwd = pwdInp.value;

        if (user && pwd) {

            var xhr = new XMLHttpRequest();

            xhr.open("post", `../php/login.php`, true);

            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(`user=${user}&pwd=${pwd}`);

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    // console.log(xhr.responseText);
                    // var data = JSON.parse(xhr.responseText);
                    // console.log(data);
                    var { status, detail } = JSON.parse(xhr.responseText);
                    if (status) {

                        if (isRem.checked) {
                            setCookie("lgc", user, 31);
                        } else {
                            setCookie("lgc", user);
                        }

                        // 登录时判断是否有returnUrl这个参数
                        var search = location.search;
                        if (search) { // 从其他页面中途跳转登录
                            var returnUrl = search.split("=")[1];
                            location.href = decodeURIComponent(returnUrl);
                        } else {  //没有参数  直接进入主页
                            location.href = "./index1.html";
                        }

                    } else {
                        alert(detail);
                    }
                }
            }


        }
    }










</script>

</html>